package com.bjsxt.common.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.util.ConstantFinal;
import com.bjsxt.common.util.EncryPasswordUtil;
import com.bjsxt.common.util.FileUtil;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.common.util.RandomUtil;
import com.bjsxt.common.util.VerifyCodeUtils;

public class BaseController
{
	@Resource
	protected FileUtil fileUtil;
	protected String info;
	@Resource
	protected EncryPasswordUtil encryPasswordUtil;
	@RequestMapping("imgCode.htm")
	public String code(HttpServletRequest req,HttpServletResponse resp)
	{
		ConstantFinal.LOGGER_SYS.info("------code");
		String code=new RandomUtil().getRandom(4);
		try
		{
			VerifyCodeUtils.outputImage(100, 50, resp.getOutputStream(), code);
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpSession session = req.getSession();
		session.setAttribute("imgCode", code);
		return null;
	}
	//通过req来得到分页对象
	protected PageInfoUtil getPageInfoUtil(HttpServletRequest req)
	{
		String currentPage = req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
		PageInfoUtil pageInfo=new PageInfoUtil();
		try
		{
			pageInfo.setCurrentPage(Integer.parseInt(currentPage));
			pageInfo.setPageSize(Integer.parseInt(pageSize));
		} catch (NumberFormatException e)
		{
		}
		return pageInfo;
	}
	//拼装jui需要响应的json
	protected JSONObject returnJson(HttpServletRequest req, String info)
	{
		//{ "statusCode":"200", "message":"\u64cd\u4f5c\u6210\u529f", "navTabId":"", "rel":"", "callbackType":"", "forwardUrl":"", "confirmMsg":"" } 
		String navTabId = req.getParameter("navTabId");
		String rel = req.getParameter("rel");
		String callbackType = req.getParameter("callbackType");
		String forwardUrl = req.getParameter("forwardUrl");
		String confirmMsg = req.getParameter("confirmMsg");
		
		if(navTabId==null){
			navTabId="";
		}
		if(rel==null){
			rel="";
		}
		if(callbackType==null){
			callbackType="";
		}
		if(forwardUrl==null){
			forwardUrl="";
		}
		if(confirmMsg==null){
			confirmMsg="";
		}
		
		JSONObject mesJson=new JSONObject();
		mesJson.put("statusCode","200");
		mesJson.put("navTabId",navTabId);
		mesJson.put("rel",rel);
		mesJson.put("callbackType",callbackType);
		mesJson.put("forwardUrl",forwardUrl);
		mesJson.put("confirmMsg",confirmMsg);
		mesJson.put("message",info);
		
		return mesJson;
	}
		
}
